home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / hurd / hurdkill.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-19  |  2.7 KB  |  84 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <sys/types.h>
  21. #include <signal.h>
  22. #include <hurd.h>
  23. #include <hurd/port.h>
  24. #include <hurd/signal.h>
  25. #include <hurd/msg.h>
  26.  
  27. /* Send a `sig_post' RPC to process number PID.  If PID is zero,
  28.    send the message to all processes in the current process's process group.
  29.    If PID is < -1, send SIG to all processes in process group - PID.
  30.    SIG and REFPORT are passed along in the request message.  */
  31. error_t
  32. _hurd_sig_post (pid_t pid, int sig, mach_port_t arg_refport)
  33. {
  34.   int delivered = 0;        /* Set when we deliver any signal.  */
  35.   error_t err;
  36.   mach_port_t proc;
  37.   struct hurd_userlink ulink;
  38.  
  39.   inline void kill_pid (pid_t pid) /* Kill one PID.  */
  40.     {
  41.       err = HURD_MSGPORT_RPC (__proc_getmsgport (proc, pid, &msgport),
  42.                   ((refport = arg_refport), 0),
  43.                   /* If no message port we cannot send signals.  */
  44.                   msgport == MACH_PORT_NULL ? EPERM :
  45.                   __sig_post (msgport, sig, refport));
  46.       if (! err)
  47.     delivered = 1;
  48.     }
  49.  
  50.   proc = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
  51.  
  52.   if (pid <= 0)
  53.     {
  54.       /* Send SIG to each process in pgrp (- PID).  */
  55.       mach_msg_type_number_t npids = 10, i;
  56.       pid_t pidsbuf[10], *pids = pidsbuf;
  57.  
  58.       err = __proc_getpgrppids (proc, - pid, &pids, &npids);
  59.       if (!err)
  60.     {
  61.       for (i = 0; i < npids; ++i)
  62.         {
  63.           kill_pid (pids[i]);
  64.           if (err == ESRCH)
  65.         /* The process died already.  Ignore it.  */
  66.         err = 0;
  67.         }
  68.       if (pids != pidsbuf)
  69.         __vm_deallocate (__mach_task_self (),
  70.                  (vm_address_t) pids, npids * sizeof (pids[0]));
  71.     }
  72.     }
  73.   else
  74.     kill_pid (pid);
  75.  
  76.   _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, proc);
  77.  
  78.   /* If we delivered no signals, but ERR is clear, this must mean that
  79.      every kill_pid call failed with ESRCH, meaning all the processes in
  80.      the pgrp died between proc_getpgrppids and kill_pid; in that case we
  81.      fail with ESRCH.  */
  82.   return delivered ? 0 : err ?: ESRCH;
  83. }
  84.